package com.weilele.leisure.time.ui.module.mine


import android.os.Bundle
import com.weilele.base.library.BaseBindingFragment
import com.weilele.leisure.time.R
import com.weilele.leisure.time.databinding.FragmentMineBinding
import com.weilele.leisure.time.ui.SettingConfig
import com.weilele.leisure.time.ui.fragment.HomeViewModel
import com.weilele.leisure.time.ui.holder.SettingClickItemHolder
import com.weilele.leisure.time.ui.holder.SettingSwitchItemHolder
import com.weilele.mvvm.adapter.MvvmRcvAdapter
import com.weilele.mvvm.adapter.addDividingWithLinearLayoutManager
import com.weilele.mvvm.adapter.refreshList
import com.weilele.mvvm.base.helper.createActivityViewModel
import com.weilele.mvvm.utils.activity.dp
import com.weilele.mvvm.utils.activity.getResColor
import com.weilele.mvvm.utils.activity.toast

/**
 * 我的界面
 */
class MineFragment : BaseBindingFragment<FragmentMineBinding>() {

    private val rcvAdapter = MvvmRcvAdapter<Any>().apply {
        register(SettingSwitchItemHolder::class.java)
        register(SettingClickItemHolder::class.java)
    }


    private val homeViewModel by lazy { createActivityViewModel<HomeViewModel>() }

    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        mBinding.title.setText(R.string.title_mine)
        mBinding.rcv.adapter = rcvAdapter
        mBinding.rcv.addDividingWithLinearLayoutManager(
            1,
            color = getResColor(R.color.colorLine),
            radius = 1f,
            startMargin = 16.dp
        )
        initRcvData()
    }

    private fun initRcvData() {
        val datas = mutableListOf<Any>()
        //region 是否展示在线影视
        if (SettingConfig.homeEnableVideoSwitchButton) {
            datas.add(
                SettingSwitchItemHolder.Bean(
                    getString(R.string.video_module),
                    SettingConfig.homeEnableVideoPager
                ) { view, isCheck ->
                    SettingConfig.homeEnableVideoPager = isCheck
                    homeViewModel.updatePages()
                    if (isCheck) {
                        R.string.video_module_open_tips.toast()
                    } else {
                        R.string.video_module_close_tips.toast()
                    }
                }
            )
        }
        //endregion
        datas.add(
            SettingClickItemHolder.Bean(getString(R.string.about_app), {
                AboutAppFragment.start(this)
            })
        )
        rcvAdapter.refreshList(datas)
    }
}